const app = getApp(), core = require('../../utils/core');
let utils = require('../../utils/proCityCountry.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    company_id:'',
    array: ['男', '女'],
    index:0,
    name:'',
    age:'',
    mobile:'',
    enterprise_id:'',
    enterprise_name:'',
    job_title:'',
    id_number:'',
    bank_number:'',
    bank_name:'',
    is_show_bank:0,
    registration: {},
  },
  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

    //银行卡是否显示
    staff_registration(){
      var that = this;
      core.get({
        url: '/index/show_bank',
        success(res) {
          that.setData({
            is_show_bank:res.data.is_show
          });
        }
      });
    },

  // 推荐劳务名称
  laowu:function(){
    wx.navigateTo({
      url: '../search_store/search_store',
    })
  },
  enterprise_name: function (e) {
    this.setData({enterprise_name: e.detail.value});
  },
  // 姓名
  name:function(e){
    this.setData({ name:e.detail.value });
  },
  // 年龄
  age: function (e) {
    this.setData({ age: e.detail.value });
  },
  // 手机
  mobile: function (e) {
    this.setData({ mobile: e.detail.value });
  },
  
  // 身份证号
  id_number: function (e) {
    this.setData({ id_number: e.detail.value });
  },
  // 银行卡号
  bank_number: function (e) {
    this.setData({ bank_number: e.detail.value });
  },
  //开户行
  bank_name:function(e){
    this.setData({ bank_name: e.detail.value });
  },
  // 确认登记
  submit:function(){
    var that = this;
    var company_id = that.data.company_id;
    var name = that.data.name;
    var sex = that.data.index;
    var age = that.data.age;
    var mobile = that.data.mobile;
    var enterprise_id = that.data.enterprise_id;
    var enterprise_name = that.data.enterprise_name;
    var id_number = that.data.id_number;
    var bank_number = that.data.bank_number;
    var bank_name = that.data.bank_name;
    if (name.replace(/(^\s*)|(\s*$)/g, "") == '') {
      wx.showToast({ title: '请输入姓名', icon: 'none' })
      return false;
    }
    // if (age < 18) {
    //   wx.showToast({ title: '请输入合法年龄', icon: 'none' })
    //   return false;
    // }
    if (!(/^(0|86|17951)?1[0-9]{10}$/.test(mobile)) || !mobile) {
      wx.showToast({ title: '手机号格式错误', icon: 'none' })
      return false;
    }
   
    if (id_number.length != 15 && id_number.length != 18) {
      wx.showToast({ title: '请输入正确的身份证号', icon: 'none' })
      return false;
    }
    if (that.data.is_show_bank == 1 && bank_name == '') {
      wx.showToast({ title: '请输入开户支行', icon: 'none' })
      return false;
    }
    if (that.data.is_show_bank == 1 && bank_number.replace(/(^\s*)|(\s*$)/g, "") == '') {
      wx.showToast({ title: '请输入银行卡号', icon: 'none' })
      return false;
    }
    core.post({
      url: '/hr/staff_registration',
      data: { company_id: company_id, name: name,sex:sex, mobile: mobile, enterprise_id: enterprise_id, id_number: id_number, bank_number: bank_number,bank_name:bank_name, enterprise_name: enterprise_name},
      success(res) {
        that.setData({ name: '',  mobile: '', enterprise_id: '', job_title: '', id_number: '', bank_number: '', enterprise_name: '',bank_name:'',bank_number:''},)
        wx.switchTab({url:'/pages/indexpage/indexpage'});
      }
    })
  },

  //获取最新提交的信息
  getRegistration() {
    core.get({url:'hr/staff_registration',success:ret=>{
      this.setData({registration: ret.data.registration});
      if(ret.data.registration) {
        this.setData({
          name: ret.data.registration.name,
          mobile: ret.data.registration.mobile,
          id_number: ret.data.registration.id_number,
          bank_name: ret.data.registration.bank_name,
          bank_number: ret.data.registration.bank_number,
          index: ret.data.registration.sex
        });
      }
    }});
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.staff_registration();
    this.getRegistration();
    this.setData({ company_id: options.company_id })
  },



  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})
